﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Security.Cryptography;
using System.Xml;
using System.Text;

namespace WebQueryRole
{
    public partial class Login : System.Web.UI.Page
    {
        private DBCollection _dbCollection = new DBCollection();
        protected string infoMessage;
        protected bool adminMode;
        protected bool justLoggedIn;
        protected LoginState loginState;

        protected void Page_Load(object sender, EventArgs e)
        {
            DBCollection db = new DBCollection();
            infoMessage = "";
            justLoggedIn = false;
            loginState = BPAuthentication.IsLoggedIn(this, db);
            adminMode = (!String.IsNullOrEmpty(Request["admin"]) && Request["admin"].ToLower().Equals("true") && loginState.IsLoggedIn);

            // if we got to this page with post data..
            string submitType = Request.Form["submit"];

            
            if (loginState.IsLoggedIn)
            {
                infoMessage = "You are logged in as: " + loginState.UserName;
            }
            if (IsPostBack && !String.IsNullOrEmpty(submitType))
            {

                if (submitType.Equals("login") && !loginState.IsLoggedIn)
                {
                    loginState = BPAuthentication.LogIn(this, Request.Form["username"], Request.Form["password"], db);
                    if (loginState.IsLoggedIn)
                    {
                        justLoggedIn = true;
                        // login successful show redirect page if URL is provided  
                        infoMessage = "Thank you for logging in.";
                        if (!String.IsNullOrEmpty(Request["url"]))
                        {
                            infoMessage += "<br/> You will be redirected to " + Request["url"] + " in about 3 seconds..";                            
                        }
                    }
                    else
                    {
                        // login failure update message text and stay on the same page
                        infoMessage = "<span style=\"color:red\"> Invalid Credentials. </span>";
                    }
                }
                else if (submitType.Equals("create"))
                {
                    if (BPAuthentication.CreateUser(Request.Form["create-username"], Request.Form["create-password"], db))
                    {
                        // user created,  return msg successful.
                        infoMessage = "User Created: " + Request.Form["create-username"] + " / " + Request.Form["create-password"];
                    }
                    else
                    {
                        // user was unable to be created
                        infoMessage = "Error, Could not create user";
                    }
                }
            }
            //BPAuthentication.CreateUser("gbc3", "test", db);


            db.CloseAll();
        }

    }
}
